import type { DeptInfo, DeptTreeNode } from '@/types/dept.type'
import type { ApiResponse } from '@/types/common.type'

// 模拟部门数据
const mockDepts: DeptInfo[] = [
  {
    id: 1,
    deptName: '总公司',
    parentId: 0,
    leader: '张三',
    phone: '13800138000',
    email: 'zhangsan@example.com',
    sort: 1,
    status: 1,
    createTime: '2024-01-01 12:00:00',
    updateTime: '2024-01-01 12:00:00',
    children: [
      {
        id: 2,
        deptName: '研发部',
        parentId: 1,
        leader: '李四',
        phone: '13800138001',
        email: 'lisi@example.com',
        sort: 1,
        status: 1,
        createTime: '2024-01-01 12:00:00',
        updateTime: '2024-01-01 12:00:00',
        children: [
          {
            id: 3,
            deptName: '前端组',
            parentId: 2,
            leader: '王五',
            phone: '13800138002',
            email: 'wangwu@example.com',
            sort: 1,
            status: 1,
            createTime: '2024-01-01 12:00:00',
            updateTime: '2024-01-01 12:00:00'
          },
          {
            id: 4,
            deptName: '后端组',
            parentId: 2,
            leader: '赵六',
            phone: '13800138003',
            email: 'zhaoliu@example.com',
            sort: 2,
            status: 1,
            createTime: '2024-01-01 12:00:00',
            updateTime: '2024-01-01 12:00:00'
          }
        ]
      },
      {
        id: 5,
        deptName: '市场部',
        parentId: 1,
        leader: '钱七',
        phone: '13800138004',
        email: 'qianqi@example.com',
        sort: 2,
        status: 1,
        createTime: '2024-01-01 12:00:00',
        updateTime: '2024-01-01 12:00:00'
      }
    ]
  }
]

// 获取部门列表
export function mockGetDeptList(params: any): Promise<ApiResponse<DeptInfo[]>> {
  const { deptName, status } = params

  let filteredList = [...mockDepts]

  if (deptName) {
    filteredList = filteredList.filter(item => item.deptName.includes(deptName))
  }
  if (status !== undefined) {
    filteredList = filteredList.filter(item => item.status === status)
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: filteredList
      })
    }, 500)
  })
}

// 获取部门树
export function mockGetDeptTree(): Promise<ApiResponse<DeptTreeNode[]>> {
  const buildTree = (parentId: number): DeptTreeNode[] => {
    return mockDepts
      .filter(item => item.parentId === parentId)
      .map(item => ({
        ...item,
        children: buildTree(item.id)
      }))
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: buildTree(0)
      })
    }, 500)
  })
}

// 创建部门
export function mockCreateDept(data: Omit<DeptInfo, 'id'>): Promise<ApiResponse<number>> {
  const newId = Math.max(...mockDepts.map(item => item.id)) + 1
  const newDept: DeptInfo = {
    ...data,
    id: newId,
    createTime: new Date().toISOString().replace('T', ' ').slice(0, 19),
    updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
  }
  mockDepts.push(newDept)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: newId
      })
    }, 500)
  })
}

// 更新部门
export function mockUpdateDept(data: DeptInfo): Promise<ApiResponse<void>> {
  const index = mockDepts.findIndex(item => item.id === data.id)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '部门不存在'
    })
  }

  mockDepts[index] = {
    ...data,
    updateTime: new Date().toISOString().replace('T', ' ').slice(0, 19)
  }

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
}

// 删除部门
export function mockDeleteDept(id: number): Promise<ApiResponse<void>> {
  const index = mockDepts.findIndex(item => item.id === id)
  if (index === -1) {
    return Promise.reject({
      code: 404,
      message: '部门不存在'
    })
  }

  // 检查是否有子部门
  const hasChildren = mockDepts.some(item => item.parentId === id)
  if (hasChildren) {
    return Promise.reject({
      code: 400,
      message: '该部门下存在子部门，无法删除'
    })
  }

  mockDepts.splice(index, 1)

  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        code: 200,
        message: 'success',
        data: undefined
      })
    }, 500)
  })
} 